/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-27，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.ui.activity;

import java.util.ArrayList;
import java.util.List;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.cache.CacheConstant;
import com.chinamobile.ln.ictenterprises.common.Constants;
import com.chinamobile.ln.ictenterprises.model.ChooseContact;
import com.chinamobile.ln.ictenterprises.ui.adapter.FragmentAdapter;
import com.chinamobile.ln.ictenterprises.ui.fragment.EntAddressBookFragment;
import com.chinamobile.ln.ictenterprises.ui.fragment.MyAddressBookFragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class ChooseContactActivity extends FragmentActivity implements
    OnClickListener
{
    private List<Fragment> list;
    private FragmentAdapter adapter;
    private ViewPager mViewPager;
    private int currIndex; // 当前页卡编号
    private RelativeLayout ly_ent_addressbook, ly_my_addressbook, ly_guide;
    private TextView tv_ent_addressbook, tv_my_addressbook;
    private Context context = this;
    private ImageView guideImageView;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_contact_layout);
        sharedPreferences = getSharedPreferences("firststart",
            Context.MODE_PRIVATE);
        setupView();
    }

    protected void setupView()
    {
        if (sharedPreferences.getBoolean("choosefirststart", true))
        {
            Editor editor = sharedPreferences.edit();
            editor.putBoolean("choosefirststart", false);
            editor.commit();
            ly_guide = (RelativeLayout) findViewById(R.id.ly_guide);
            ly_guide.setVisibility(View.VISIBLE);
            ly_guide.getBackground().setAlpha(100);
            guideImageView = (ImageView) findViewById(R.id.ok);
            guideImageView.setOnClickListener(this);
        }
        ly_ent_addressbook = (RelativeLayout) findViewById(R.id.ly_ent_addressbook);
        ly_my_addressbook = (RelativeLayout) findViewById(R.id.ly_my_addressbook);
        ly_ent_addressbook.setOnClickListener(this);
        ly_my_addressbook.setOnClickListener(this);
        tv_ent_addressbook = (TextView) findViewById(R.id.tv_ent_addressbook);
        tv_my_addressbook = (TextView) findViewById(R.id.tv_my_addressbook);
        TextView ok_btn = (TextView) findViewById(R.id.ok_btn);
        ok_btn.setOnClickListener(this);
        ImageView back_btn = (ImageView) findViewById(R.id.back_btn);
        back_btn.setOnClickListener(this);
        list = new ArrayList<Fragment>();
        list.add(new EntAddressBookFragment());
        list.add(new MyAddressBookFragment());
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new FragmentAdapter(getSupportFragmentManager(), list);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0)
            {
                currIndex = arg0;
                if (currIndex == 0)
                {
                    tv_ent_addressbook.setTextColor(getResources().getColor(
                        R.color.white));
                    tv_my_addressbook.setTextColor(getResources().getColor(
                        R.color.blue));
                    ly_ent_addressbook
                        .setBackgroundResource(R.drawable.table_sel_left);
                    ly_my_addressbook
                        .setBackgroundResource(R.drawable.table_bg_right);
                }
                else
                {
                    tv_my_addressbook.setTextColor(getResources().getColor(
                        R.color.white));
                    tv_ent_addressbook.setTextColor(getResources().getColor(
                        R.color.blue));
                    ly_my_addressbook
                        .setBackgroundResource(R.drawable.table_sel_right);
                    ly_ent_addressbook
                        .setBackgroundResource(R.drawable.table_bg_left);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {

            }
        });
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.ok_btn:
            List<ChooseContact> list = new ArrayList<ChooseContact>();
            list.addAll(CacheConstant.chooseEntList);
            list.addAll(CacheConstant.chooseMyList);
            if (list.size() > 20)
            {
                Toast.makeText(context, "选择地址本不能大于20个!", Toast.LENGTH_SHORT)
                    .show();
                return;
            }
            if (list.size() > 0)
            {
                Intent data = new Intent();
                data.putParcelableArrayListExtra("dataList",
                    (ArrayList<? extends Parcelable>) list);
                setResult(Constants.RESULT_OK, data);
                finish();
            }
            break;
        case R.id.back_btn:
            finish();
            break;
        case R.id.ly_ent_addressbook:
            mViewPager.setCurrentItem(0);
            break;
        case R.id.ly_my_addressbook:
            mViewPager.setCurrentItem(1);
            break;
        case R.id.ok:
            ly_guide.setVisibility(View.GONE);
            break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        CacheConstant.chooseEntList.clear();
        CacheConstant.chooseMyList.clear();
        if (list != null)
        {
            list.clear();
            list = null;
        }
        adapter = null;
    }
}
